let globalData = getApp().globalData;
const qiniu = require("../../utils/qnutils");
var {
    http
  } = require('../../utils/apiutils');
Page({
    timer: null,
    innerAudioContext:null,
    data: {
        bgUrl:'http://linghuoimage.xbbzp.com/Fup_8DW1a-WF1KEUvE1JPsmX-VcF',
        underway: false,  //true为正在录制
        dao:5,
        loading:false,
        url:'',
        token:''
    },
    onLoad() {
        this.ctx = wx.createCameraContext();
    },
    onShow(){
        console.log(7777)
        // this.getUpToken()
    },
    onReady(){
		let timer = setInterval(() => {
            if(this.data.dao==1){
                clearInterval(timer)
                this.phone();
                return 
            }
            this.setData({dao:this.data.dao-1})
		}, 1000);
    },
    
    error(e) {
        console.log(e.detail)
    },
    play(){
        this.audioCtx.play()
    },
    pause(){
        this.audioCtx.pause()
    },
    phone(){
        let _this  =this;
        this.ctx.takePhoto({
            success(res){
                _this.setData({
                    url:res.tempImagePath
                })
                wx.setStorageSync('hasPai', res.tempImagePath)
                wx.showToast({
                    title: '抓拍成功，3s后返回视频页',
                    icon:'none',
                    duration:2000,
                    success(){
                        let timer = setTimeout(()=>{
                            clearTimeout(timer);
                            wx.navigateBack({
                                delta:1
                            })
                        },3000)
                    }
                })
                // _this.upDataImage(res.tempImagePath);
            }
        })
    },
    getUpToken: function () {
        var that = this;
        http.getUpToken({},
          function (res) {
            that.data.token = res.data;
            qiniu.initQiniu(res.data);
          },
          function (err) {
          })
    },
    upDataImage: function (tempFilePaths,index) {
        var that = this;
        qiniu.getUploadImgae(tempFilePaths,
          function (res) {
                wx.setStorageSync('hasPai', res.imageURL);
                wx.showToast({
                    title: '抓拍成功，3s后返回视频页',
                    icon:'none',
                    duration:2000,
                    success(){
                        let timer = setTimeout(()=>{
                            clearTimeout(timer);
                            wx.navigateBack({
                                delta:1
                            })
                        },3000)
                    }
                })
          },
          function (err) {
              console.log('图片上传报错')
          })
      },
})